package kocla.com.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 作者：池锦标 on 2016/9/22 11:50
 * 邮箱：292284583@qq.com
 */
public class CircleView extends View {
    //    private RectF mBigOval;
    private Paint paint = new Paint();
    RectF oval = new RectF();
    //内环半径
    private float innerCircleRadius = 100;
    //外环半径
    private float outCircleRadius = 120;
    private final static float MAX_SIZE = 300;
    private final static float MIN_SIZE = 48;

    private int progressColor = Color.RED;

    public CircleView(Context context) {
        super(context);
    }

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        obtainStyledAttrs(attrs);
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    private void obtainStyledAttrs(AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.PrgoressStyleable);
        progressColor = ta.getColor(R.styleable.PrgoressStyleable_progress_color, Color.RED);
        ta.recycle();
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int centreX = getWidth() / 2; //获取圆心的x坐标
        int centreY = getHeight() / 2; //获取圆心的y坐标
        calcuateBeforeDraw();
        paint.setAntiAlias(true);                       //设置画笔为无锯齿
        paint.setColor(Color.BLACK);                    //设置画笔颜色

        //画圆弧
        //画圆弧的时候要注意RectF类的用法，左上右下这四个点是以原点为中心，
        // 边和上边减圆的半径，右边和下边是加圆的半径，这样就可以构成圆弧的范围了。
        paint.setColor(progressColor);
        paint.setStrokeWidth((float) outCircleRadius - innerCircleRadius);              //线宽
        paint.setStyle(Paint.Style.STROKE);
        //RectF对象
        oval.left = centreX - ((outCircleRadius - innerCircleRadius) / 2 + innerCircleRadius);                        //左边
        oval.top = centreY - ((outCircleRadius - innerCircleRadius) / 2 + innerCircleRadius);                                   //上边
        oval.right = centreX + ((outCircleRadius - innerCircleRadius) / 2 + innerCircleRadius);                             //右边
        oval.bottom = centreY + ((outCircleRadius - innerCircleRadius) / 2 + innerCircleRadius);                                //下边
        if (mProgress > 0) {
            canvas.drawArc(oval, 0, 90 + mProgress, false, paint);    //绘制圆弧
        } else {
            canvas.drawArc(oval, 0, 90, false, paint);    //绘制圆弧
        }


        paint.setColor(Color.BLACK);
        paint.setStrokeWidth((float) 3.0);
        //画内环
        canvas.drawCircle(centreX, centreY, innerCircleRadius, this.paint);
        //画外环
        canvas.drawCircle(centreX, centreY, outCircleRadius, this.paint);


        //---------------------------------------------------------------------
        // 画大矩形
      /*  mBigOval = new RectF(40, 10, 280, 250);// 初始化较大的矩形
        canvas.drawRect(mBigOval, paint);
        // 在大矩形分别 画4个圆弧图形
        //画圆弧的时候要注意RectF类的用法，左上右下这四个点是以原点为中心，
        // 边和上边减圆的半径，右边和下边是加圆的半径，这样就可以构成圆弧的范围了。
        RectF oval1 = new RectF();                     //RectF对象
        oval1.left = 160 - 120;                              //左边
        oval1.top = 130 - 120;                                   //上边
        oval1.right = 160 + 120;                             //右边
        oval1.bottom = 130 + 120;                                //下边
        canvas.drawArc(oval1, 0, 360, false, paint);    //绘制圆弧*/

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int minSize = Math.min(widthSize, heightSize);
        if (minSize > MAX_SIZE) {
            minSize = (int) MAX_SIZE;
        }
        setMeasuredDimension(minSize, minSize);
        Log.i("CircleView", "widthSize = " + widthSize + ",heightSize = " + heightSize);

    }


    private void calcuateBeforeDraw() {
        innerCircleRadius = getMeasuredWidth() / 2 - 30;
        outCircleRadius = innerCircleRadius + 20;
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }


    private int mProgress = 0;

    public void setProgress(int progress) {
        this.mProgress = progress;
        if (progress > 270) {
            return;
        }
        handler.obtainMessage(1).sendToTarget();
    }

    public int getProgress() {
        return mProgress;
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == 1) {
                invalidate();
            }

        }
    };

}
